package com.simplehc7878.auth.security;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;

import java.util.Map;

public class SecurityCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 获取application.yml中配置的enableConfig值
        String enabledConfig = context.getEnvironment().getProperty("custom.enableConfig");

        if (!StringUtils.hasText(enabledConfig)) {
            return false;
        }
        Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(SecurityConditionOn.class.getName());
        Class<?> configClass = (Class<?>) annotationAttributes.get("value");

        return enabledConfig.equalsIgnoreCase(configClass.getSimpleName());
    }
}
